@file:Suppress("unused")

package com.wh1200.xmlless.test.dao

import com.wh1200.mybatis.xmlless.XmlLessMapper
import com.wh1200.mybatis.xmlless.annotations.SelectedProperties
import com.wh1200.mybatis.xmlless.annotations.TestCriteria
import com.wh1200.mybatis.xmlless.annotations.XmllessMethod
import com.wh1200.xmlless.test.model.User
import com.wh1200.xmlless.test.model.UserSimple
import com.wh1200.xmlless.test.vo.UserVO
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Param
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable

/**
 *
 * @author 吴昊
 * @since 0.0.1
 */
@Mapper
interface UserDAO : XmlLessMapper<User> {

  /**
   *
   * @return
   */
  fun count(): Int

  /**
   *
   * @param id
   */
  fun deleteById(id: Int)

  /**
   * @param ids
   */
  fun deleteByIdIn(ids: List<Int>)

  /**
   *
   * @param pageable
   * @return
   */
  fun findAll(@Param("pageable") pageable: Pageable): Page<User>

  /**
   * @param type
   * @param name
   * @return
   */
  @XmllessMethod(
      conditions = [
        "name is not null or departmentId is not null",
        "name = '2' or roles CONTAINS 2",
        "name in names",
        "name in ('1','2')"
      ]
  )
  @SelectedProperties(["id", "name", "departmentId", "departmentName"])
  fun find666(
      @TestCriteria(
          ifExpression = "type = 1",
          sqlExpression = "roleListName = 'a' or roleListName = 'b'"
      )
      @Param("type") type: Int,
      @TestCriteria(
          ifExpression = "name == null or name == ''",
          sqlExpression = "name is null or name = ''"
      )
      @Param("name") name: String
  ): List<UserVO>

  @XmllessMethod(
      conditions = [
        "roles contains any roles"
      ]
  )
  fun findByAnyRole(roles: List<Int>): List<User>

  /**
   * @param id
   * @return
   */
  fun findStudentNameById(@Param("id") id: Int): String?

  /**
   * @param id
   * @return
   */
  @XmllessMethod(
      includes = ["student.email"],
      conditions = ["id", "student.name LIKE keywords"]
  )
  fun findStudentNameById2(@Param("id") id: Int, @Param("keywords") keywords: String): String?

  /**
   * @param pageable
   * @return
   */
  @XmllessMethod
  fun findAll2(@Param("pageable") pageable: Pageable): List<User>

  /**
   * @return
   */
  @XmllessMethod
  fun findAll3(): List<User>

  /**
   *
   * @return
   */
  @SelectedProperties(["name"])
  fun findAllNames(): List<String>

  /**
   * @param id
   * @return
   */
  fun findRoleListById(id: Int): User?

  /**
   *
   * @param id
   * @return
   */
  fun findById(id: Int): User?

  /**
   *
   * @param id
   * @return
   */
  @XmllessMethod(conditions = ["id"])
  fun findSimpleUserById(id: Int): UserSimple

  /**
   *
   * @param user
   */
  fun save(user: User)

  /**
   *
   * @param user
   */
  fun saveAll(user: List<User>)

  /**
   * @param user
   */
  fun saveBatch(user: List<User>)

  /**
   *
   * @param user
   */
  fun update(user: User)

  /**
   * @param id
   * @param role
   * @return
   */
  fun updateRolesRemoveRoleById(
      @Param("id") id: Int, @Param("role") role: Int
  ): Int

  /**
   * @param id
   * @param role
   * @return
   */
  fun updateRolesAddRoleById(
      @Param("id") id: Int, @Param("role") role: Int
  ): Int

  /**
   * @param id
   * @return
   */
  fun updateRolesSetEmptyById(@Param("id") id: Int): Int

  /**
   * @param id
   * @return
   */
  fun updateDepartmentIdSetNullById(@Param("id") id: Int): Int

}

